import React, { useState } from 'react';

import { Form, Input, Modal, Select, Upload,message } from 'antd';
import { FormValueType, TreeNode } from '../data.d';
import { LoadingOutlined, PlusOutlined } from '@ant-design/icons';
import styles from '../index.less';
const FormItem = Form.Item;
const { Option } = Select;
export interface UpdateFormProps {
  onCancel: () => void;
  onSubmit: (values: FormValueType) => void;
  updateModalVisible: boolean;
  values: FormValueType;
  treeData?:API.TreeNodeItem[];
  
}

const formLayout = {
  labelCol: { span: 5 },
  wrapperCol: { span: 15 },
};

const UpdateForm: React.FC<UpdateFormProps> = (props) => {
  const [formVals, setFormVals] = useState<FormValueType>(props.values);
 
  const [form] = Form.useForm();
  const [loading, setLoading] = useState<boolean>(false);
  const { updateModalVisible, onSubmit: handleUpdate,  onCancel, treeData} = props;

  const okHandle = async () => {
    const fieldsValue = await form.validateFields();
  
    fieldsValue.typeId= formVals.typeId;
    fieldsValue.dictCode= formVals.dictCode;
    fieldsValue.picCode= formVals.picCode;
    form.resetFields();
    console.log(fieldsValue)
    handleUpdate(fieldsValue);
  };
 
  const handleChange = (info: any) => {
    if (info.file.status === 'uploading') {
      setLoading(true);
      return;
    }
    console.log(info.file);
    if (info.file.status === 'done') {
      // Get this url from response in real world.
      const picCode = info.file.response.attachCode;
      console.log(picCode);
      setFormVals({ ...formVals, ...{ picCode: picCode } });
      setLoading(false);
    }
  };

  const beforeUpload = (file: any) => {
    const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
    if (!isJpgOrPng) {
      message.error('只能上传JPG/PNG文件!');
    }
    const isLt2M = file.size / 1024 / 1024 < 2;
    if (!isLt2M) {
      message.error('图像必须小于2MB!');
    }
    return isJpgOrPng && isLt2M;
  }

  const uploadButton = (
    <div>
      {loading ? <LoadingOutlined /> : <PlusOutlined />}
      <div className="ant-upload-text">上传图片</div>
    </div>
  );


  return (
    <Modal
      width={640}
     
      destroyOnClose
      title="编辑字典"
      visible={updateModalVisible}
      onOk={okHandle}
      onCancel={() => onCancel()}
    >
     <Form
        {...formLayout}
        form={form}
        initialValues={{
          dictCode: formVals.dictCode,
          dictValue: formVals.dictValue,
          typeId: formVals.typeId.toString(),
          dictDescription: formVals.dictDescription,
          deleteFlag: formVals.deleteFlag,
          picCode: formVals.picCode,
        
      
        }}
      >
        <FormItem
          label="字典类型"
          name="typeId"
        >
           <Select placeholder="请选择">
            {treeData.map((item) => (
              <Option key={item.value} value={item.value}>
                {item.title}
              </Option>
           ))}
            </Select>
        
        </FormItem>
        <FormItem
          label="字典编码"
          name="dictCode"
         // rules={[{ required: true, message: '请输入至少三个字符！', min: 3 }]}
        >
          <Input placeholder="请输入" disabled />
        </FormItem>
        <FormItem
          label="字典名称"
          name="dictValue"
          rules={[{ required: true, message: '请输入字典名称！' }]}
        >
          <Input placeholder="请输入" />
        </FormItem>
      
        <FormItem
          label="图标"
          name="picCode"
         
        >
           <Upload
                name="file"
                listType="picture-card"
                showUploadList={false}
                className={styles.uploader}
               // headers={{ 'x-auth-token': getToken() }}
                action="/api/imageUpload"
                beforeUpload={beforeUpload}
                onChange={handleChange}
              >
                {formVals.picCode ? <img src={'/img/'  + formVals.picCode} alt="avatar" style={{ width: '100%' }} /> : uploadButton}
              </Upload>
        </FormItem>
        <FormItem
          label="描述"
          name="dictDescription"
        >
          <Input.TextArea rows={2} placeholder="请输入" />
        </FormItem>
        
        {/* <FormItem
          label="是否禁用"
          name="deleteFlag"
        >
          <Radio.Group>
            <Radio value={0} >
              否
            </Radio>
            <Radio value={1}>
              是
            </Radio>
          </Radio.Group>
        </FormItem> */}
      </Form>
    </Modal>
  );
};

export default UpdateForm;
